package com.okhttp.put;

import com.alibaba.fastjson2.JSONObject;
import okhttp3.*;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
 * @author: ywy
 * @date: 2022-11-13
 * @desc:
 */
public class ClientPut {
    static OkHttpClient client = new OkHttpClient();

    public static void putWithFile() {

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", "test.txt",
                        RequestBody.create(new File("/Users/Issac/2022/workspaces/personal/" +
                                        "demo-2022/http-demo/src/main/resources/test.txt"),
                                MediaType.parse("application/octet-stream")))
                .build();
        Request request = new Request.Builder()
                .url("http://localhost:8080/upload")
                .put(requestBody)
                .build();
        try {
            Response response = client.newCall(request).execute();
            System.out.println("[headers]" + response.headers());
            System.out.println("[status code]" + response.code());
            System.out.println("[body]" + Objects.requireNonNull(response.body()).string());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        putWithFile();
    }
}
